﻿using Architecture.Domain;
using Architecture.Domain.Dto;
using Architecture.Repository;
using Architecture.Repository.InterfaceRabbit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.Service.Rabbit
{
    public class TraceService : ITraceService
    {
        protected readonly IRepository<Goods> Gooda;
        protected readonly ITraceabilityRepository _raceRepository;
        protected readonly IFarmingRepository _farmingRepository;
        protected readonly IHealthRepository _healthRepository;
        protected readonly IBusinessRepository _businessRepository;
        protected readonly IPhotoRepositroy _photoRepositroy;
        protected readonly ISlaughterRepository _saughterRepository;
        protected readonly IProcessingRepository _processingRepository;

        //构造函数
        public TraceService(ITraceabilityRepository raceRepository, IRepository<Goods> gooda, IFarmingRepository farmingRepository, IHealthRepository healthRepository, IBusinessRepository businessRepository, IPhotoRepositroy photoRepositroy, ISlaughterRepository saughterRepository, IProcessingRepository processingRepository)
        {
            _raceRepository = raceRepository;
            Gooda = gooda;
            _farmingRepository = farmingRepository;
            _healthRepository = healthRepository;
            _businessRepository = businessRepository;
            _photoRepositroy = photoRepositroy;
            _saughterRepository = saughterRepository;
            _processingRepository = processingRepository;
        }
        #region 溯源
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(Traceability model)
        {
            model.AddName = "张三";
            model.AddTime = DateTime.Now;
            return await _raceRepository.AddAsync(model);
        }
        //溯源列表
        public async Task<object> GetAllAsync()
        {
            var goodlist=await Gooda.GetAllAsync();
            var tracelist= await _raceRepository.GetAllAsync();
            var list = (from t in tracelist
                        join g in goodlist on t.GoodId equals g.GoodId
                        select new
                        {
                            t.GoodId,t.TraceabilityId,t.TraceabilityBatch,
                            t.TraceabilityDay,t.TraceabilityNumber,
                            t.TraceabilityLicense,t.AddName,t.AddTime,
                            t.UpdateName,g.GoodName
                        }).ToList();
            list=list.OrderByDescending(x=>x.TraceabilityId).ToList();
            return list;
        }
        //商品列表
        public async Task<List<Goods>> GetGooda()
        {
            return await Gooda.GetAllAsync();
        }
        //删除
        public async Task<int> DeleteAsync(int id)
        {
            return await _raceRepository.isDelte(id);
        }
        //查看详情
        public async Task<Traceability> GetIdAsync(int id)
        {
            return await _raceRepository.GetAsync(id);
        }
        //修改
        public async Task<int> UpdateAsync(Traceability model)
        {
            var trace=await _raceRepository.GetAsync(model.TraceabilityId);
            trace.GoodId = model.GoodId;
            trace.TraceabilityBatch = model.TraceabilityBatch;
            trace.TraceabilityDay = model.TraceabilityDay;
            trace.TraceabilityNumber = model.TraceabilityNumber;
            trace.TraceabilityLicense = model.TraceabilityLicense;
            trace.TraceabilityIamnges = model.TraceabilityIamnges;
            trace.UpdateName = "李四";
            trace.UpdateTime = DateTime.Now;
            return await _raceRepository.UpdateAsync(trace);
        }
        #endregion

        #region 养殖
        //养殖添加
        public async Task<int> AddFarming(Farming mode)
        {
            return await _farmingRepository.AddAsync(mode);
        }
        //养殖列表
        public async Task<object> GetFarming(int id)
        {
            var farming=await _farmingRepository.GetAllAsync();
            var list= farming.Where(x=>x.TraceabilityId==id).ToList();
            return list;
        }
        #endregion

        #region 保健
        //保健添加
        public async Task<int> AddHealth(Health mode)
        {
            return await _healthRepository.AddAsync(mode);
        }
        //保健列表
        public async Task<object> GetHealth(int id)
        {
            var Health = await _healthRepository.GetAllAsync();
            var list = Health.Where(x => x.TraceabilityId == id).ToList();
            return list;
        }
        //保健查看详情
        public async Task<Health> GetAllHealth(int id)
        {
            return await _healthRepository.GetAsync(id);
        }
        /// <summary>
        /// 保健修改实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> UpdateHealth(Health model)
        {
            var health = await _healthRepository.GetAsync(model.HealthId);
            health.HealthDay = model.HealthDay;
            health.HealthWay = model.HealthWay;
            health.HealthMethods = model.HealthMethods;
            return await _healthRepository.UpdateAsync(health);
        }
        /// <summary>
        /// 删除、真
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteHealth(int id)
        {
            return await _healthRepository.isDelte(id);
        }
        #endregion

        #region 企业
        //企业添加
        public async Task<int> AddBusiness(Business mode)
        {
            return await _businessRepository.AddAsync(mode);
        }
        //企业列表
        public async Task<object> GetBusiness(int id)
        {
            var business = await _businessRepository.GetAllAsync();
            var list = business.Where(x => x.TraceabilityId == id).ToList();
            return list;
        }
        //修改企业
        public async Task<int> UpdateBusiness(Business mode)
        {
            var business =await _businessRepository.GetAsync(mode.BusinessId);
            business.CompanyName = mode.CompanyName;
            business.BusinessDate = mode.BusinessDate;
            business.BusinessAddress = mode.BusinessAddress;
            business.BusinessProfile = mode.BusinessProfile;
            return await _businessRepository.UpdateAsync(business);
        }
        #endregion

        //产品检验报告图片
        public async Task<int> AddPhoto(Photo mode)
        {
            return await _photoRepositroy.AddAsync(mode);
        }
        //产品检验图删除
        public async Task<int> DeletePhoto(string? url)
        {
            var list = (await _photoRepositroy.GetAllAsync()).Where(x=>x.PhotoImge==url);
            var res = 0;
            foreach (var item in list)
            {
                res = await _photoRepositroy.DeleteAsync(item);
            }
            return res;
        }
        //检验图片详情
        public async Task<List<PhotoDto>> GetPhoto(int id)
        {
            var photo=(from p in await _photoRepositroy.GetAllAsync() where p.TraceabilityId==id select new PhotoDto
            {
                Url=p.PhotoImge
            }).ToList();
            return photo;
        }
        //动检图详情
        public async Task<List<PhotoDto>> GetDPhoto(int id)
        {
            var photo = (from p in await _photoRepositroy.GetAllAsync()
                         where p.FarmingId == id
                         select new PhotoDto
                         {
                             Url = p.PhotoImge
                         }).ToList();
            return photo;
        }
        //企业图片详情
        public async Task<List<PhotoDto>> GetQPhoto(int id)
        {
            var photo = (from p in await _photoRepositroy.GetAllAsync()
                         where p.BusinessId == id
                         select new PhotoDto
                         {
                             Url = p.PhotoImge
                         }).ToList();
            return photo;
        }
        //资质图详情
        public async Task<List<PhotoDto>> GetZPhoto(int id)
        {
            var photo = (from p in await _photoRepositroy.GetAllAsync()
                         where p.SlaughterId == id
                         select new PhotoDto
                         {
                             Url = p.PhotoImge
                         }).ToList();
            return photo;
        }
        //食品图详情
        public async Task<List<PhotoDto>> GetSPhoto(int id)
        {
            var photo = (from p in await _photoRepositroy.GetAllAsync()
                         where p.ProcessingId == id
                         select new PhotoDto
                         {
                             Url = p.PhotoImge
                         }).ToList();
            return photo;
        }
        #region 屠宰
        //屠宰添加
        public async Task<int> AddSlaughter(Slaughter mode)
        {
            return await _saughterRepository.AddAsync(mode);
        }
        //屠宰列表
        public async Task<object> GetSlaughter(int id)
        {
            var farming = await _saughterRepository.GetAllAsync();
            var list = farming.Where(x => x.TraceabilityId == id).ToList();
            return list;
        }
        #endregion
        #region 深加工
        //深加工添加
        public async Task<int> Addprocessing(Processing mode)
        {
            return await _processingRepository.AddAsync(mode);
        }
        //深加工显示
        public async Task<object> Getprocessing(int id)
        {
            var farming = await _processingRepository.GetAllAsync();
            var list = farming.Where(x => x.TraceabilityId == id).ToList();
            return list;
        }
        #endregion
    }
}
